package com.shuqi.activity.viewport;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.shuqi.common.utils.SharedPreferenceUtil;
import com.shuqi.controller.R;

public class UpdateDialog extends Dialog {

    private Context context;

    private String dialogTitle;

    private String dialogText;
    
    private int newVersion;

    private String dialogLeftBtn;

    private String dialogRightBtn;

    private View.OnClickListener rightBtnOnClickListener;

    private View.OnClickListener leftBtnOnClickListener;

    public UpdateDialog(Context context) {
        super(context);
        this.context = context;
    }

    public UpdateDialog(Context context, int theme, String dialogTitle, String dialogText,int newVersion,
            String dialogLeftBtn, String dialogRightBtn) {
        super(context, theme);
        this.context = context;
        this.dialogTitle = dialogTitle;
        this.dialogText = dialogText;
        this.dialogLeftBtn = dialogLeftBtn;
        this.dialogRightBtn = dialogRightBtn;
        this.newVersion=newVersion;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.view_dialog_update);

        TextView dialogTitle = (TextView) findViewById(R.id.dialogTitle);
        dialogTitle.setText(this.dialogTitle);

        TextView dialogText = (TextView) findViewById(R.id.dialogText);
        dialogText.setText(this.dialogText);

        TextView dialogLeftBtn = (TextView) findViewById(R.id.dialogLeftBtn);
        dialogLeftBtn.setText(this.dialogLeftBtn);

        TextView dialogRightBtn = (TextView) findViewById(R.id.dialogRightBtn);
        dialogRightBtn.setText(this.dialogRightBtn);
        
        CheckBox checkBox = (CheckBox) findViewById(R.id.dialogCheckBox);
        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton button, boolean isCheck) {
                if (isCheck) {// 不再提示
                    SharedPreferenceUtil.setNoPromptPushVersion(context, newVersion);
                } else {// 继续提示
                    SharedPreferenceUtil.setNoPromptPushVersion(context, Integer.MIN_VALUE);
                }

            }
        });
        
        if (leftBtnOnClickListener != null) {
            dialogLeftBtn.setOnClickListener(leftBtnOnClickListener);
        } else {
            dialogLeftBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UpdateDialog.this.cancel();
                }
            });
        }

        if (rightBtnOnClickListener != null) {
            dialogRightBtn.setOnClickListener(rightBtnOnClickListener);
        } else {
            dialogRightBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {}
            });
        }

    }

    public void setRightBtnOnClickListener(View.OnClickListener rightBtnOnClickListener) {
        this.rightBtnOnClickListener = rightBtnOnClickListener;
    }

    public void setLeftBtnOnClickListener(View.OnClickListener leftBtnOnClickListener) {
        this.leftBtnOnClickListener = leftBtnOnClickListener;
    }

}
